package sky.dao.Impl;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import sky.dao.PlaneDAO;
import sky.domain.Plane;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 21.04.13
 * Time: 23:18
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class PlaneDAOImpl implements PlaneDAO {
    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void addPlane(Plane plane) {
        sessionFactory.getCurrentSession().save(plane);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Plane> listPlane() {
        return sessionFactory.getCurrentSession().createQuery("from Plane").list();
    }

    @Override
    public void removePlane(Integer id) {
        Plane plane = (Plane) sessionFactory.getCurrentSession().load(Plane.class, id);
        if (null != plane) {
            sessionFactory.getCurrentSession().delete(plane);
        }
    }

    @Override
    public Plane getPlaneById(Integer id) {
        return (Plane) sessionFactory.getCurrentSession().get(Plane.class, id);
    }

    @Override
    public void editPlane(Plane plane) {
        sessionFactory.getCurrentSession().update(plane);
    }
}
